package com.auz.datagenerator.heuristics.time.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.auz.datagenerator.heuristics.time.SetIntervalHeuristic;

/**
 * The GUI component for the set interval heuristics
 * @author Aurelien Bonnel
 */
public class SetIntervalHeuristicPanel extends JPanel implements ChangeListener, ActionListener {

	private boolean paused = true;
	private SetIntervalHeuristic parent;
	private JButton launchButton;
	private JSlider intervalSlider;
	
	/**
	 * Constructor
	 * @param _parent - the associated interval heuristic
	 */
	public SetIntervalHeuristicPanel(SetIntervalHeuristic _parent) {
		parent = _parent;
		setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		JLabel title = new JLabel("Set Interval Controls");
		add(title, BorderLayout.NORTH);
		launchButton = new JButton("Launch");
		launchButton.addActionListener(this);
		panel.add(launchButton);
		JLabel label = new JLabel("Interval (s)");
		panel.add(label);
		intervalSlider = new JSlider(JSlider.HORIZONTAL, 0, 30, _parent.getInterval() / 1000);
		intervalSlider.setMajorTickSpacing(10);
		intervalSlider.setMinorTickSpacing(1);
		intervalSlider.setPaintLabels(true);
		intervalSlider.setPaintTicks(true);
		intervalSlider.addChangeListener(this);
		panel.add(intervalSlider);
		add(panel, BorderLayout.CENTER);
		setVisible(true);
	}

	/**
	 * Called if the state of the slider is changed
	 * @param _e - the event causing this method to be called
	 */
	public void stateChanged(ChangeEvent _e) {
		if(_e.getSource() == intervalSlider) {
			if(! intervalSlider.getValueIsAdjusting()) {
				if(intervalSlider.getValue() == 0)
					parent.setInterval(100);
				else
					parent.setInterval(intervalSlider.getValue() * 1000);
			}
		}
	}

	/**
	 * Called if an action is performed on the pause/unpause button
	 * @param _e - the event causing this method to be called
	 */
	public void actionPerformed(ActionEvent _e) {
		if(_e.getSource() == launchButton) {
			if(paused){
				paused = false;
				launchButton.setLabel("Pause");
				parent.unpause();
			} else if(! paused) {
				paused = true;
				launchButton.setLabel("Resume");
				parent.pause();
			}
		}
	}
	
}
